﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Engine;
using Game.Server.Event;
using Game.Server.PlayerEvent;
using System.Collections.Generic;
using System.IO;
using System;
using Game.NetWork.Packages;
using System.Reflection;
using Game.Server;
using Newtonsoft.Json;
using Game.NetWork;
using System.Text;
using System.Security.Cryptography;
using LiteNetLib;


namespace Game.Server.Plugins
{
    public class 创造插件 : AbstractProcessCmd, IUpdateable
    {
        public override string Cmd => "gamemode";// 命令

        public override string Introduce => "/gamemode   - 创造插件";// 描述

        public override int AuthLevel => 1000;// 权限等级

        public override DisplayType Display => DisplayType.All; // 指令是否可查看

        string 输入 = "";//输入

        public SubsystemGameInfo SubsystemGameInfo;//游戏信息组件

        public ComponentPlayer ComponentPlayer;//玩家组件

        public override void ProcessCmd()// 处理命令
        {
            var method = new 方法集合();
            SubsystemGameInfo = GameManager.Project.FindSubsystem<SubsystemGameInfo>();// 获取游戏信息组件
            ComponentPlayer = this.m_client.PlayerData.ComponentPlayer;// 获取玩家组件
            SubsystemPlayers subsystemPlayers = GameManager.Project.FindSubsystem<SubsystemPlayers>();// 获取玩家管理组件
            if (GameManager.Project == null)
            {
                SendMessage(Cmd, "请先进入存档");
                return;
            }
            if (m_messageDatas.Length < 2)
            {
                SendMessage(Cmd, "\n /gamemode 0 - 全局游戏模式切换为创造模式\n/gamemode 1 - 全局游戏模式切换为生存模式 \n/gamemode 2 - 全局游戏模式切换为冒险模式 \n/gamemode 3 - 全局游戏模式切换为无害模式 \n/gamemode 4 - 全局游戏模式切换为挑战模式");
                return;
            }
            输入 = m_messageDatas[1]; //获取输入

                foreach (PlayerData playersDatum in subsystemPlayers.PlayersData)//遍历玩家
                {
                    switch (输入)
                    {
                        case "0":
                            SubsystemGameInfo.WorldSettings.GameMode = GameMode.Creative;
                            SendMessage(Cmd, $"{ComponentPlayer.PlayerData.Name}已将全局游戏模式切换为创造模式");
                            method.SendMsgPack(playersDatum.Client.PlayerData.ComponentPlayer, "已将全局游戏模式切换为创造模式");//发送消息
                            CommonLib.Net.RemoveClient(playersDatum.Client, $"{ComponentPlayer.PlayerData.Name}已将全局游戏模式切换为创造模式");
                            Log.Information($"{ComponentPlayer.PlayerData.Name}已将全局游戏模式切换为创造模式");
                        break;
                        case "1":
                            SubsystemGameInfo.WorldSettings.GameMode = GameMode.Survival;
                            SendMessage(Cmd, $"{ComponentPlayer.PlayerData.Name}已将全局游戏模式切换为生存模式");
                            method.SendMsgPack(playersDatum.Client.PlayerData.ComponentPlayer, "已将全局游戏模式切换为生存模式");//发送消息
                            CommonLib.Net.RemoveClient(playersDatum.Client, $"{ComponentPlayer.PlayerData.Name}已将全局游戏模式切换为生存模式");   
                            Log.Information($"{ComponentPlayer.PlayerData.Name}已将全局游戏模式切换为生存模式");
                        break;
                    case "2":
                        SubsystemGameInfo.WorldSettings.GameMode = GameMode.Adventure;
                        SendMessage(Cmd, $"{ComponentPlayer.PlayerData.Name}已将全局游戏模式切换为冒险模式");
                        method.SendMsgPack(playersDatum.Client.PlayerData.ComponentPlayer, "已将全局游戏模式切换为冒险模式");//发送消息
                        CommonLib.Net.RemoveClient(playersDatum.Client, $"{ComponentPlayer.PlayerData.Name}已将全局游戏模式切换为冒险模式");
                        Log.Information($"{ComponentPlayer.PlayerData.Name}已将全局游戏模式切换为冒险模式");
                        break;
                    case "3":
                        SubsystemGameInfo.WorldSettings.GameMode = GameMode.Harmless;
                        SendMessage(Cmd, $"{ComponentPlayer.PlayerData.Name}已将全局游戏模式切换为无害模式");
                        method.SendMsgPack(playersDatum.Client.PlayerData.ComponentPlayer, "已将全局游戏模式切换为无害模式");//发送消息
                        CommonLib.Net.RemoveClient(playersDatum.Client, $"{ComponentPlayer.PlayerData.Name}已将全局游戏模式切换为无害模式");
                        Log.Information($"{ComponentPlayer.PlayerData.Name}已将全局游戏模式切换为无害模式");
                        break;
                    case "4":
                        SubsystemGameInfo.WorldSettings.GameMode = GameMode.Challenging;
                        SendMessage(Cmd, $"{ComponentPlayer.PlayerData.Name}已将全局游戏模式切换为挑战模式");
                        method.SendMsgPack(playersDatum.Client.PlayerData.ComponentPlayer, "已将全局游戏模式切换为挑战模式");//发送消息
                        CommonLib.Net.RemoveClient(playersDatum.Client, $"{ComponentPlayer.PlayerData.Name}已将全局游戏模式切换为挑战模式");
                        Log.Information($"{ComponentPlayer.PlayerData.Name}已将全局游戏模式切换为挑战模式");
                        break;
                    default:
                           method.SendMsgPack(playersDatum.Client.PlayerData.ComponentPlayer, $"{ComponentPlayer.PlayerData.Name}无效的指令");//发送消息
                           SendMessage(Cmd, $"无效的指令");
                           Log.Information($"[gamemode]{ComponentPlayer.PlayerData.Name}无效的指令");
                        break;
                    }
                }

        }

        public UpdateOrder UpdateOrder => UpdateOrder.Default;

        public void Update(float dt)// 更新
        {

        }
    }
    public class 方法集合
    {
        public void DisplayLargeMessageNet(ComponentPlayer componentPlayer, string text, string more, float duration = 2, float delay = 0) // 标题信息方法
        {
            var messagePackget = new MessagePackage(text, more, duration, delay);
            messagePackget.To = componentPlayer.PlayerData.Client;
            CommonLib.Net.QueuePackage(messagePackget);
        }
        public void load(ComponentPlayer componentPlayer, string text) // 读取方法
        {
            var messagePackget = new MessagePackage(null, text, 0, null);
            messagePackget.To = componentPlayer.PlayerData.Client;
            CommonLib.Net.QueuePackage(messagePackget);
        }
        public void DisplaySmallMessageNet(ComponentPlayer componentPlayer, string text)  // 小消息方法
        {
            var messagePackget = new MessagePackage(null, text, 0, null);
            messagePackget.To = componentPlayer.PlayerData.Client;
            CommonLib.Net.QueuePackage(messagePackget);
        }
        public void SendMsgPack(ComponentPlayer componentPlayer, string text)  //发送消息方法
        {
            MessagePackage messagePackage = new MessagePackage(null, text, 0, null);
            messagePackage.To = componentPlayer.PlayerData.Client;
            CommonLib.Net.QueuePackage(messagePackage);
        }
       
    }

    public class gamemode : ServerPlugin, IExplodeEventHandle  // 继承ServerPlugin 类，实现IExplodeEventHandle接口
    {
        public override string Name => "更改全局游戏模式插件"; // 插件名称

        public override int Version => 1000;// 插件版本

        public byte FirstLevel => 100;// 初始等级

        public void Explode(int x, int y, int z, ref float pressure, bool isIncendiary, bool noExplosionSound, PlayerData miner) // 爆炸事件
        {
            
        }

        public override void Initialize()// 初始化
        {
            ExplodeEventManager.AddObject(this);  // 注册爆炸事件
        }

        public override void Load()
        {
            
        }

        public override void Save()
        {
           
        }

    }

}
